import 'package:aqua_plunge/dao/broadcaster_dao.dart';
import 'package:aqua_plunge/http/app_manager.dart';
import 'package:aqua_plunge/util/toast_util.dart';
import 'package:aqua_plunge/widget/app_bar.dart';
import 'package:aqua_plunge/widget/custom_avatar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../controller/DatabaseListController.dart';
import '../controller/UserController.dart';
import '../model/listItem.dart';

class BlockListPage extends StatefulWidget {
  const BlockListPage({super.key});

  @override
  State<BlockListPage> createState() => _BlockListPageState();
}

class _BlockListPageState extends State<BlockListPage> {

  final DatabaseController _databaseController = Get.find<DatabaseController>();
  final UserController _userController = Get.find<UserController>();
  bool loading = false;

  get _listview => Container(
        padding: const EdgeInsets.all(24),
        decoration: const BoxDecoration(
            image: DecorationImage(
                image: AssetImage('images/img_primary_bg.png'),
                fit: BoxFit.cover)),
        child: Obx((){
          return ListView.builder(
              itemCount: _userController.blockList.length,
              itemBuilder: (context, index) {
                return _buildListItem(_userController.blockList[index]);
              });
        }),
      );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: appBar("Block List"),
        body: Stack(
          children: [
            _listview,
            Visibility(visible:loading,child:const Center(child: CircularProgressIndicator()))
          ],
        ));
  }

  Widget _buildListItem(listItem item) {
    return Container(
        margin: const EdgeInsets.only(top: 10, bottom: 10),
        width: double.infinity,
        padding: const EdgeInsets.only(left: 12, right: 24),
        height: 80,
        decoration: const BoxDecoration(
            image: DecorationImage(
                image: AssetImage('images/list_item_bg.png'),
                fit: BoxFit.cover)),
        child: Row(
          children: [
           CustomAvatar(width: 48, height: 48, netPath: item.avatar ?? ''),
            const SizedBox(width: 8),
            Expanded(
                child: FittedBox(
                  fit: BoxFit.scaleDown,
                  alignment: Alignment.centerLeft,
                  child: Text(
                    item.nickName ?? "",
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: const TextStyle(color: Colors.white, fontSize: 14),
                  ),
                )),
            const SizedBox(width: 8),
            GestureDetector(
                onTap: () {
                  _unblock(item.broadcasterId ?? "");
                },
                child: Image.asset(
                  "images/icon_block.png",
                  width: 24,
                  height: 24,
                ))
          ],
        ));
  }

  void _unblock(String broadcasterId) {
    setState(() {
      loading = true;
    });
    BroadCasterDao.removeBlock(int.parse(broadcasterId), (result) async{
      if (result is bool && result == true) {
        await AppManager.removeBlockList(int.parse(broadcasterId));
        _databaseController.fetchCreationData();
        _userController.updateFollowBlock();
        _databaseController.fetchMediaData();
        appToast.showTipLong('UnBlock Successfully');
      }
      setState(() {
        loading = false;
      });
    });
  }
}
